package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func OrderCreate(context *gin.Context) {
	var l request.OrderCreate
	if err := context.ShouldBind(&l); err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return

	}
	r, err := config.AppointmentClient.OrderCreate(config.Ctx, &__.OrderCreateReq{
		Num:           l.Num,
		PaymentMethod: l.PaymentMethod,
		UserId:        l.UserId,
		ShopId:        l.ShopId,
		Price:         float32(l.Price),
	})
	if err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "账号禁用",
			"data": err.Error(),
		})
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "订单更新成功",
		"data": r.Id,
	})
}

func OrderUserList(context *gin.Context) {
	var l request.Orderlist
	if err := context.ShouldBind(&l); err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return

	}
	r, err := config.AppointmentClient.UserOrderList(config.Ctx, &__.UserOrderListReq{
		Page:     int64(l.Page),
		Pagesize: int64(l.PageSize),
	})
	if err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "账号禁用",
			"data": err.Error(),
		})
		return
	}
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "我的订单展示成功",
		"data": r,
	})
	return
}

func OrderUpdate(context *gin.Context) {
	var l request.UpdateOrder
	if err := context.ShouldBind(&l); err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return

	}
	_, err := config.AppointmentClient.OrderUpdate(config.Ctx, &__.OrderUpdateReq{
		Num:           l.Num,
		PaymentMethod: l.PaymentMethod,
		UserId:        l.UserId,
		ShopId:        l.ShopId,
		Price:         float32(l.Price),
		Id:            l.Id,
		Storck:        l.Storck,
	})
	if err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "账号禁用",
			"data": err.Error(),
		})
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code": 400,
		"msg":  "不能超过500库存数量",
		//"data": r.Id,
	})
}

func DelOrder(context *gin.Context) {
	var l request.OrderDel
	if err := context.ShouldBind(&l); err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return

	}
	r, err := config.AppointmentClient.DelOrder(config.Ctx, &__.OrderDelReq{

		Id: l.Id,
	})
	if err != nil {
		context.JSON(400, gin.H{
			"code": 400,
			"msg":  "不能超过500库存数量",
			"data": err.Error(),
		})
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "取消成功",
		"data": r.Id,
	})
}
